package com.yibo.user.test;

import com.yibo.redis.RedisCommand;
import com.yibo.redis.util.lock.LockUtil;
import com.yibo.redis.util.lock.RedisLock;
import com.yibo.support.helper.JsonHelper;
import com.yibo.db.user.entity.User;
import org.junit.Test;

import javax.annotation.Resource;

public class RedisTemplate extends BaseTest {

    @Resource
    private RedisCommand redisCommand;

    @Test
    public void setRedisTest(){
        User user = new User()
                .setUserId("10003")
                .setUserName("张三")
                .setPassword("123456");

        boolean result = redisCommand.set("10003",JsonHelper.toJson(user));
        assert  result;
        User value = redisCommand.get("10003",User.class);
        System.out.println(value);
    }

    @Test
    public void  lockTest(){
        for(int i=0 ; i<10 ; i++) {
            RedisLock redisLock = LockUtil.getLock("12345");
            boolean acq = redisLock.acquire();
            if(acq) {
                System.out.println("获取到锁了");
                //释放锁
                redisLock.release();
            }

        }
    }
}
